#include "servicedefine.h"
//#include "Config.h"
#include "../GitStoreMeta/MetaObject.h"
#include "../GitStoreMeta/StoreObject.h"
#include "../GitStoreMeta/DatasetController.h"
#include "../GitStoreMeta/ObjectController.h"
//#include "StoreObject.h"
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <iostream>

using namespace std;
using namespace gitstore;

void RunServer() {
    std::string server_address("0.0.0.0:9008");
    GitStoreGrpcServiceImpl service;
    grpc::EnableDefaultHealthCheckService(true);
    grpc::reflection::InitProtoReflectionServerBuilderPlugin();

    grpc::ServerBuilder builder;
    // Listen on the given address without any authentication mechanism.
    int port = 1;
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    cout<<port<<endl;
    // Register "service" as the instance through which we'll communicate with
    // clients. In this case it corresponds to an *synchronous* service.
    builder.RegisterService(&service);
    // Finally assemble the server.
    std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;

    // Wait for the server to shutdown. Note that some other thread must be
    // responsible for shutting down the server for this call to ever return.
    server->Wait();
}

StoreObject OO() {

    auto x = StoreObject("jk");
    return x;
}
class A {
public:
    A(){cout<<"c a"<<endl;}
    ~A(){}
};

int main() {
    RunServer();
    vector<A> va;
    va.reserve(100);
    string sss("dfd\0dfe", 9);
    cout<<sss.size()<<sss.c_str()[5]<<endl;
//    RunServer();
    StoreObject valuek = OO();

    vector<StoreObject> values;
    values.emplace_back("haha11");
    values.emplace_back("haha22");
    StoreObject value1("haha1");
    value1 = OO();
    StoreObject value2("haha2");
    StoreObject value3("haha11");

    CDatasetController op;
    gitstore::Branch branch;
    int revision_ds,revision_branch;
    op.createDataset("ds60","","master","",static_cast<StoreObjectType>(0),vector<string>{"1","10"},values,revision_ds,revision_branch);
//    op.getBranchInfo("ds2", "master", branch);
//    auto res = op.branchDataset("ds2", "master", "dev", "");
//    res = op.getBranchInfo("ds2", "dev", branch);
//    res = op.putKey("ds2", "dev", "2", value1, branch.cur_version);
//    int revision_branch;
    auto res = op.getBranchInfo("ds60", "master", branch, revision_branch);
    auto res1 = op.putKey("ds60", "master", "1", value3,  revision_branch, revision_branch);
    vector<string> vds,vbr,vks;
//    op.getKeyByBranch("ds4", "master", "1", value1);
    res = op.getDiffKeysBranch("ds6", "master", "master", vks, revision_branch, revision_branch);
//

//    op.listDataset(vds);
//    op.listDatasetBranches("ds2",vbr);
//    op.listDatasetKeysByBranch("ds2","master",vks);
//    StoreObject value3;
//    op.getKeyByBranch("ds2", "master", "10", value3);
//    CObjectController op;
//    gitstore::Object obj;

////    op.createObject("test3","","master","","",value);
//    op.getObjectInfo("test3",obj);
//    op.getValueByBranch("test3", "master", value);
//    StoreObject value;
//    CDatasetController op;
//    vector<string> vds;
////    op.listDataset(vds);
//    op.listDatasetBranches("test", vds);
//    op.listDatasetKeys("test", "master", vds);
//    op.branchDataset("test", "master", "dev3", "");
//    op.putKey("test", "dev3", "2", StoreObject("ttkk"), "");
//    op.putKey("test", "dev3", "3", StoreObject("ttkf"), "");
////    op.putKey("test", "dev1", "2", StoreObject("sskk"), "");
//    op.listDatasetKeys("test", "dev3", vds);
//    auto res = op.create("test", "test", "master", "test", 0);
//    CObjectController op;

    return 0;
}
